package view;

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Observable;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import controller.ACSystem;
import model.Auction;
import model.Item;
import model.NonProfit;
import model.WestPanelEnum;

/** 
 * The auction pane 
 * @author Bob Landowski
 */
@SuppressWarnings("serial")
public class AuctionPane extends JPanel {

	/** The organization */
	private String my_organization;
	
	/** The first name */
	private String my_first_name;
	
	/** The last name */
	private String my_last_name;
	
	/** The phone */
	private String my_phone;
	
	/** The auction */
	private Auction my_auction = new Auction(null, null, null, null);
	
	/** The  date */
	private String my_date;
	
	/** The aution number */
	private int my_auction_number;
	
	/** The  start time */
	@SuppressWarnings("unused")
	private String my_start_time;
	
	/** The  end time */
	@SuppressWarnings("unused")
	private String my_end_time;
	
	/** The  owner */
	@SuppressWarnings("unused")
	private NonProfit my_owner;
	
	/** The  items */
	@SuppressWarnings("unused")
	private ArrayList<Item> my_items;
	
	/** The  duration */
	private int my_duration;
	
	/** The  number of items */
	private int my_numberOfItems;
	
	/** The  ACcontroller */
	private ACSystem my_ACcontroller;
	
	/** The  start time */
	private String my_start_time_string;
	
	/** The  end time */
	private String my_end_time_string;

	/** The  contact name label */
	private JLabel my_contact_name;

	/** The organization name label */
	private JLabel my_organization_name;
	
	/** The  contact phone label */
	private JLabel my_contact_phone;
	
	/** The  duration label */
	private JLabel durationLabel;
	
	/** The  auction date label */
	private JLabel auctionDateLabel;
	
	/** The  start time label */
	private JLabel starttimeLabel;
	
	/** The  end time label */
	private JLabel endTimeLabel;
	
	/** The  number of items labels */
	private JLabel numOfItemsField;
	
	/** Shows auction number */
	private JLabel my_auctionNumberField;
	
	@SuppressWarnings("rawtypes")
	private JList itemList;
	
	/** The  west panel observable */
	@SuppressWarnings("unused")
	private WestPanelObservable wpo = new WestPanelObservable();

	private JLabel estimateNumItemsField;

	/**
	 * The auction pane constructor 
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public AuctionPane(final ACSystem the_ACcontroller) {
		setBorder(new LineBorder(new Color(0, 0, 0), 5));
		my_ACcontroller = the_ACcontroller;
		setLayout(null);

		JLabel auctiondisplayLabel = new JLabel(
				"Auction Central Auction Display");
		auctiondisplayLabel.setBounds(81, 16, 233, 20);
		add(auctiondisplayLabel);

		setPreferredSize(new Dimension(500, 500));

		JLabel organizationLabel = new JLabel("Organization");
		organizationLabel.setBounds(15, 76, 99, 20);
		add(organizationLabel);

		my_organization_name = new JLabel(my_organization);
		my_organization_name.setBounds(159, 76, 173, 20);
		my_organization_name.setText(my_organization);
		add(my_organization_name);

		JLabel contactPersonLabel = new JLabel("Contact person:");
		contactPersonLabel.setBounds(15, 106, 117, 20);
		add(contactPersonLabel);

		my_contact_name = new JLabel(my_first_name + " " + my_last_name);
		my_contact_name.setBounds(159, 106, 198, 20);
		add(my_contact_name);

		JLabel contactPhoneLabel = new JLabel("Contact Phone:");
		contactPhoneLabel.setBounds(15, 136, 117, 20);
		add(contactPhoneLabel);

		my_contact_phone = new JLabel(my_phone);
		my_contact_phone.setBounds(159, 136, 135, 20);
		add(my_contact_phone);

		JLabel dateLabel = new JLabel("Auction Date:");
		dateLabel.setBounds(15, 166, 99, 20);
		add(dateLabel);

		JLabel auctionStartTimeLabel = new JLabel("Auction start time:");
		auctionStartTimeLabel.setBounds(15, 196, 146, 20);
		add(auctionStartTimeLabel);

		JLabel numberOfItemsLabel = new JLabel(
				"Number of items:");
		numberOfItemsLabel.setBounds(15, 286, 146, 20);
		add(numberOfItemsLabel);

		auctionDateLabel = new JLabel(my_date);
		auctionDateLabel.setBounds(132, 166, 88, 20);
		add(auctionDateLabel);


		starttimeLabel = new JLabel(my_start_time_string);
		starttimeLabel.setBounds(159, 196, 61, 20);
		add(starttimeLabel);

		endTimeLabel = new JLabel(my_end_time_string);
		endTimeLabel.setBounds(380, 196, 55, 20);
		add(endTimeLabel);

		JLabel auctionDurationLabel = new JLabel("Auction duration (in hours):");
		auctionDurationLabel.setBounds(15, 226, 205, 20);
		add(auctionDurationLabel);

		numOfItemsField = new JLabel();
		numOfItemsField.setBounds(159, 286, 51, 20);
		add(numOfItemsField);

		JLabel auctionEndTimeLabel = new JLabel("Auction End Time:");
		auctionEndTimeLabel.setBounds(235, 196, 135, 20);
		add(auctionEndTimeLabel);

		durationLabel = new JLabel(my_duration + "");
		durationLabel.setBounds(225, 226, 69, 20);
		add(durationLabel);
		
		itemList = new JList(my_auction.getListModel());
		itemList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		itemList.setLayoutOrientation(JList.VERTICAL);
		itemList.setVisibleRowCount(-1);
		itemList.addListSelectionListener(new WestPanelListen());

		itemList.setBounds(16, 346, 437, 118);
		add(itemList);
		
		JLabel itemDisplayLabel = new JLabel("Items:");
		itemDisplayLabel.setBounds(16, 316, 69, 20);
		add(itemDisplayLabel);
		
		JLabel auctionNumberLabel = new JLabel("Auction Number:");
		auctionNumberLabel.setBounds(15, 46, 135, 20);
		add(auctionNumberLabel);
		
		my_auctionNumberField = new JLabel(String.valueOf(my_auction_number));
		my_auctionNumberField.setBounds(159, 46, 117, 20);
		add(my_auctionNumberField);
		
		JLabel extimateNumOfItemsLabel = new JLabel("Estimated number of items:");
		extimateNumOfItemsLabel.setBounds(15, 256, 217, 20);
		add(extimateNumOfItemsLabel);
		
		estimateNumItemsField = new JLabel();
		estimateNumItemsField.setBounds(225, 256, 69, 20);
		add(estimateNumItemsField);
	}

	/** 
	 * The getObjectData method 
	 */
	@SuppressWarnings("unchecked")
	public void getObjectData(Object the_object) {
		my_auction = (Auction) the_object;
		my_contact_name.setText(my_auction.getOwner().get_first_name() + " " + my_auction.getOwner().get_last_name());
		my_contact_phone.setText(my_auction.getOwner().getMy_phone());
		my_date =  my_auction.getDate().toString();
		auctionDateLabel.setText(my_auction.getDate().toString());
		my_auctionNumberField.setText(my_auction.getAuctionNumber()+"");
		estimateNumItemsField.setText(my_auction.getRequestedAmount() +"");
		my_numberOfItems = my_auction.getItemList().size();
		numOfItemsField.setText(my_numberOfItems+"");
		starttimeLabel.setText(my_auction.getStartTime());
		endTimeLabel.setText(my_auction.getEndTime());
		my_organization_name.setText(my_auction.getOwner().getMy_organization());
		my_items = my_auction.getItemList();
		durationLabel.setText(Integer.toString(my_auction.getDuration()));
		itemList.setModel(my_auction.getListModel());
	}
	
	private class WestPanelListen implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent arg0) {
			if (!itemList.isSelectionEmpty()) {
				my_ACcontroller.setWestPanel(WestPanelEnum.ITEM);
				my_ACcontroller.getMainFrame().westPanelState(
					my_auction.getListModel().getElementAt((itemList.getSelectedIndex())));
			}
			itemList.clearSelection();
		}

		
		
	}
	
	/** 
	 * WestPanelObservable class 
	 */
	private class WestPanelObservable extends Observable {
		
		/** 
		 * WestPanelObservable constructor 
		 */
		WestPanelObservable() {
			super();
		}
		
		
		@SuppressWarnings("unused")
		void buttonClicked(Object o) {
			setChanged();
			notifyObservers(o);
			System.out.println("notified");
		}
	}
}